package com.project.controller.web;

import com.project.entity.Image;
import com.project.myBeans.R;
import com.project.service.ImagesService;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CryFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 18:13
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/image")
@Api(description = "图片接口", value = "image")
public class ImageController extends BaseService {
    @Autowired
    ImagesService imagesService;

    @Autowired
    private CryFileUtil cryFileUtil;

    final static int ENCRY=1;//需要加密

    /**
     * TateBrown 2018年5月26日
     *
     * @param image
     * @return
     */
    @ApiOperation("新建图片信息")
    @PostMapping("/add")
    public R add(@RequestBody Image image){
        try{
            imagesService.add(image);
            return R.ok().put("data",image.getId());
        }catch (Exception e){
            return R.error("文件存储失败");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询字符串")
    @GetMapping("/find/{id}")
    public R find(@PathVariable("id")Integer id){
        try{
            Image image=imagesService.findById(id);
            if(1==image.getCategory()){//为营业执照
                String absolutePath = configParameters.getServer() + configParameters.getBusinessSubPath() + image.getPath();
                String base64=imagesService.dec(absolutePath);//base64编码的图片数据
                return R.ok().put("data",base64);
            }
            else{
                return R.ok().put("data",image.getPath());
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
